const { pool, add, edit } = require('./baseModel.js')

exports.add = async (fields) => {
    return await add('shop_cart', fields)
}

exports.edit = async (fields, where) => {
    console.log(fields, where);
    return await edit('shop_cart', fields, where)
}

exports.delete = async (id, uid) => {
    return edit('shop_cart', { isDel: 1 }, { id, uid })
}

exports.findByUid = async (uid, pageIndex) => {
    let result = await pool.query("select s.*,f.food_name,f.price,f.food_img,f.food_desc from shop_cart as s left join food_info as f on s.fid=f.id where uid=? and s.isDel=0 order by s.id desc limit ?,10", [uid, (pageIndex - 1) * 10])
    console.log(result[0]);
    return result[0]
}

exports.findAllByUid = async (uid) => {
    let result = await pool.query("select count(*) as totalCount,sum(s.count*f.price) as price  from shop_cart as s left join  food_info as f on s.fid=f.id where s.uid=? and s.isDel=0;", [uid])
    return result[0]
}

exports.checkFidExist = async (uid, fid) => {
    let result = await pool.query("select * from shop_cart where uid=? and fid=? and isDel=0;", [uid, fid])
    return result[0]
}

exports.setDefault = async (uid, fields) => {
    // console.log(Object.values(fields));
    let ids = Object.values(fields)
    if (ids.length === 0) {
        let result = await pool.query("update shop_cart set isCheck=0 where isDel=0 and uid=?", [uid, ids])
        return result[0]
    } else {
        let result = await pool.query("update shop_cart set isCheck=1 where isDel=0 and uid=? and id in (?);", [uid, ids])
        await pool.query("update shop_cart set isCheck=0 where isDel=0 and uid=? and id not in (?);", [uid, ids])
        return result[0]
    }
}
